﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace Trade.DAL
{
    public static class Utility
    {
        public enum CRUD
        {
            Insert,
            Update,
            Delete,
            None
        }

        public static string CRUDAction(CRUD crud)
        {
            string action = string.Empty;

            switch (crud)
            {
                case CRUD.Insert:
                    action = "A";
                    break;
                case CRUD.Update:
                    action = "C";
                    break;
                case CRUD.Delete:
                    action = "D";
                    break;
                case CRUD.None:
                    action = "";
                    break;
            }

            return action;
        }

        /// <summary>
        /// 取得指定日期所在月度的第一天
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static DateTime CalMinDate(DateTime date)
        {
            return DateTime.Parse(date.ToString("yyyy-MM-01"));
        }

        /// <summary>
        /// 取得指定日期所在月度的最后一天
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static DateTime CalMaxDate(DateTime date)
        {
            return DateTime.Parse(date.ToString("yyyy-MM-01")).AddMonths(1).AddDays(-1);
        }

        /// <summary>
        /// 求得指日期是星期几
        /// </summary>
        /// <param name="date"></param>
        /// <returns>e.g 星期日</returns>
        public static string ChineseDayOfWeek(DateTime date)
        {
            string[] day = new string[] { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };

            return day[Convert.ToInt16(date.DayOfWeek)];
        }

        //由DataReader转换在DataTable表结构
        public static DataTable ConvertToDataTable(SqlDataReader dataReader)
        {
            DataTable dt = new DataTable();

            try
            {
                for (int i = 0; i < dataReader.FieldCount; i++)
                {
                    DataColumn dc = new DataColumn();
                    dc.DataType = dataReader.GetFieldType(i);
                    dc.ColumnName = dataReader.GetName(i);
                    dt.Columns.Add(dc);
                }

                return dt;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            } 
        }

        //将DataReader结果中的数据加载到相应的DataTable中
        public static void AddDataToDataTable(SqlDataReader dataReader, DataTable dataTable)
        {
            try
            {
                while (dataReader.Read())
                {
                    DataRow dr = dataTable.NewRow();
                    for (int i = 0; i < dataReader.FieldCount; i++)
                    {
                        dr[i] = dataReader[i].ToString();
                    }
                    dataTable.Rows.Add(dr);
                    dr = null;
                }

                dataReader.Close(); 
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }

        //将新的DataRow中的数据更新到源的DataRow中
        public static void UpdateToDataRow(DataRow soureDataRow, DataRow destDataRow)
        { 
            
        }

        // test 生成JSON字符串
        public static string GetTasks(Guid functionId)
        {
            DataTable dt = SQLHelper.ExecuteDataTable(SQLHelper.connectionString, CommandType.Text, "select FunctionID, ParentID, Name, URL from SysFunction", (SqlParameter[])null);
            
            return "[" + GetTasksString(functionId, dt) + "]";
        }

        private static string GetTasksString(Guid functionId, DataTable dt)
        {
            DataRow[] rows = dt.Select("ParentID='" + functionId.ToString() + "'");

            if (rows.Length == 0) return string.Empty; ;
            StringBuilder str = new StringBuilder();

            foreach (DataRow row in rows)
            {
                str.Append("{");
                for (int i = 0; i < row.Table.Columns.Count; i++)
                {
                    if (i != 0) str.Append(",");
                    str.Append("\"");
                    str.Append(row.Table.Columns[i].ColumnName);
                    str.Append("\"");
                    str.Append(":\"");
                    str.Append(row[i].ToString());
                    str.Append("\"");
                }
                str.Append(",children:[");
                str.Append(GetTasksString((Guid)row["FunctionId"], dt));
                str.Append("]},");
            }
            return str[str.Length -1] == ',' ? str.ToString(0, str.Length - 1) : str.ToString();
            //return str.ToString();
        }
    }
}
